package cn.group.hotel2.service.impl;

import cn.group.hotel2.ex.ServiceException;
import cn.group.hotel2.mapper.RoomMapper;
import cn.group.hotel2.mapper.TypeMapper;
import cn.group.hotel2.pojo.dto.RoomAddNewDTO;
import cn.group.hotel2.pojo.dto.RoomUpdateDTO;
import cn.group.hotel2.pojo.entity.Room;
import cn.group.hotel2.pojo.vo.RoomListItemVO;
import cn.group.hotel2.pojo.vo.RoomStandardVO;
import cn.group.hotel2.pojo.vo.RoomUpdateVO;
import cn.group.hotel2.pojo.vo.TypeStandardVO;
import cn.group.hotel2.service.IRoomService;
import cn.group.hotel2.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class RoomServiceImpl implements IRoomService {

    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private TypeMapper typeMapper;

    @Override
    public List<RoomListItemVO> list(Integer current) {
        log.debug("跳过的记录数:{}",current);
        log.debug("列表对象:{}",roomMapper.list(current));
        return roomMapper.list(current);
    }

    @Override
    public void addNew(RoomAddNewDTO roomAddNewDTO){
        RoomStandardVO standardByRoomNum = roomMapper.getStandardByRoomNum(roomAddNewDTO.getRoomNum());

        if (standardByRoomNum!=null){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"重复的房间号");
        }
        RoomStandardVO standardByTitle = roomMapper.getStandardByTitle(roomAddNewDTO.getTitle());
        if (standardByTitle!=null){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"重复的房间标题");
        }
        Room room = new Room();
        BeanUtils.copyProperties(roomAddNewDTO,room);
        roomMapper.insert(room);
    }


    @Override
    public void updateById(Long id, RoomUpdateDTO roomUpdateDTO) {
        Room room = new Room();
        BeanUtils.copyProperties(roomUpdateDTO, room);

        log.info("roomUpdate对象:{}",roomUpdateDTO);

        String typeName = roomUpdateDTO.getTypeName();
        TypeStandardVO standardByTypeName = typeMapper.getStandardByTypeName(typeName);
        log.info("分类对象:{}",standardByTypeName);
        if (standardByTypeName == null) {
            String message = "分类不存在,请查询后再输入!";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }


        String title = roomUpdateDTO.getTitle();
        int byTitle = roomMapper.getByTitle(title);
        if (byTitle > 0) {
            String message = "该房间标题已存在,请重新输入!";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }


        String roomNum = roomUpdateDTO.getRoomNum();
        RoomStandardVO standardByRoomNum = roomMapper.getStandardByRoomNum(roomNum);
        if (standardByRoomNum != null) {
            String message = "该房间号已存在,请重新输入!";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        room.setTypeId(standardByTypeName.getId());
        log.info("room对象:{}", room);
        roomMapper.update(room);
    }

    @Override
    public RoomStandardVO getStandardById(Long id) {
        return roomMapper.getStandardById(id);
    }

    @Override
    public RoomUpdateVO getUpdateStandardById(Long id) {
        return roomMapper.getUpdateStandardById(id);
    }

    @Override
    public void deleteById(Long id) {
        roomMapper.deleteById(id);
    }

    @Override
    public int getCount() {
        return roomMapper.count();
    }
}
